package com.jy.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jy.mapper.CheckItemMapper;
import com.jy.pojo.CheckItem;
import com.jy.utils.QueryPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CheckItemService {
    @Autowired
    CheckItemMapper checkItemMapper;

    public void add(CheckItem checkItem) {
        checkItemMapper.insert(checkItem);
    }

    public Page<CheckItem> findPage(QueryPageBean qb) {
        //1.创建分页对象
        Page<CheckItem> page = new Page<>(qb.getCurrentPage(), qb.getPageSize());
        //2.创建条件对象
        LambdaQueryWrapper<CheckItem> qw = new LambdaQueryWrapper<>();
        //3.对一筐多查进行判断
        if (StringUtils.isNotBlank(qb.getQueryString())) {
            qw.eq(CheckItem::getCode, qb.getQueryString());
            qw.or();
            qw.like(CheckItem::getName, qb.getQueryString());
        }
        //4.进行排序
        qw.orderByAsc(CheckItem::getId);
        //5.进行sql语句的执行
        return checkItemMapper.selectPage(page, qw);
    }

    public void delete(Integer id) {
        checkItemMapper.deleteById(id);
    }

    public CheckItem findById(Integer id) {
        return checkItemMapper.selectById(id);
    }

    public void edit(CheckItem checkItem) {
        checkItemMapper.updateById(checkItem);
    }

    public List<CheckItem> findAll() {
        return checkItemMapper.selectList(null);
    }
}
